package com.java.onlineshop.mvc.handler;

import com.java.onlineshop.pojo.*;
import org.junit.jupiter.api.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: onlineshop
 * @description: 订单基本逻辑
 * @author: ShuangChenYue
 * @create: 2021-06-13 19:27
 **/
public class OrderHandler
{
    public void toOrder(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击结算来此订单显示方法");
            //首先得到用户的购物车
            ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
            //将总价设置进请求域中
            req.setAttribute("total",shopCart.getTotal());
            //随机生成订单编号，将其设置进request域中
            int orderNo = (int) (Math.random()*1000000*1000000);
            req.setAttribute("orderNo",orderNo);
            //转发至显示页面
            req.getRequestDispatcher("/order.jsp").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================提交订单============================================================
    public void submitOrder(HttpServletRequest req, HttpServletResponse resp)
    {
        System.out.println("点击提交订单来此方法");
        /*
        所谓提交订单，就是将shopCart中的东西插入进myorder表和orderitem表中。
         */
        //首先获取到所需要的数据，而后调用OrderStatusServiceImpl.addMyOrder(MyOrder myOrder);的方法。
        MyOrder myOrder = new MyOrder();
        //首先需要得到用户
        User user = (User) req.getSession().getAttribute("user");
        //得到用户名，也就是myorder的name
        String name = user.getName();
        //然后得到购物车
        ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
        //通过购物车得到总价钱，也就是myorder中的cost
        double total = shopCart.getTotal();
        //由于之前没有做payway和orderstatus，所以这里直接给死
        PayWay payWay = new PayWay();
        payWay.setPaywayid(1);

        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderstatusid(1);

        //依次将数据设置给myorder
        myOrder.setName(name);
        myOrder.setCost(total);
        myOrder.setUser(user);
        myOrder.setPayWay(payWay);
        myOrder.setOrderStatus(orderStatus);

        //再给orderitem设置数据
        //而后调用添加myorder方法，将数据插入进myorder和orderitem这两张表
    }
}
